<template>
  <div class="order-item-card" v-if="props.data?.id">
    <van-card 
      class="order-card"
      :price="props.data?.price"
      :desc="props.data?.shopper"
      :title="props.data?.title"
      :thumb="props.data?.img"
    >
      <template #tags>
        <div class="order-tags" v-if="props.data?.label?.length">
          <van-tag 
            v-for="(tag, index) in props.data.label" 
            :key="index" 
            plain 
            type="primary"
            size="small"
            class="order-tag"
          >
            {{ tag }}
          </van-tag>
        </div>
      </template>
      
      <template #footer>

        <div class="num">
          x{{ props.data?.num }}
        </div>
        <div class="order-footer">
          <div class="confirm-section" @click.stop>
            <van-checkbox 
              v-model="isChecked"
              checked-color="#ee0a24"
              @change="handleConfirmChange"
              class="confirm-checkbox"
            >
              <span class="confirm-text">确认</span>
            </van-checkbox>
          </div>
        </div>
        <div class="purchase-status">
          {{ props.data?.isPaid ? '已支付' : '未支付' }}
        </div>
      </template>
    </van-card>
  </div>
  <div v-else class="order-item-error"></div>
</template>

<script setup>
defineOptions({
    name: 'OrderListItem'
})
import { ref, watch } from 'vue'
// 组件props定义
const props = defineProps({
  data: {
    type: Object,
    required: true,
    default: () => ({})
  },
  type: {
    type: String,
    default: 'all'   // 分为 all pending finished
  }
})

// 组件事件定义
const emit = defineEmits(['confirm-change'])

// 响应式数据
const isChecked = ref(false)

// 监听父组件传递的数据变化，同步确认状态
watch(() => props.data, (newData) => {
  if (newData && newData.isConfirmed !== undefined) {
    isChecked.value = Boolean(newData.isConfirmed)
  }
}, { immediate: true, deep: true })

// 处理方法
const handleConfirmChange = (value) => {
  emit('confirm-change', {
    item: props.data,
    isConfirmed: value
  })
}

// 点击卡片处理（暂时为空函数，避免未定义错误）
const handleCardClick = (shopper) => {
  // TODO: 实现点击商家逻辑
  console.log('点击商家:', shopper)
}

</script>

<style scoped lang="scss">
.order-item-card {
  margin-bottom: 12px;
  
  &:last-child {
    margin-bottom: 0;
  }
  
  .order-card {
    border-radius: 12px;
    overflow: hidden;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
    transition: all 0.3s ease;
    
    &:hover {
      box-shadow: 0 4px 16px rgba(0, 0, 0, 0.12);
      transform: translateY(-2px);
    }
    
    &:deep(.van-card__header) {
      padding: 12px;
    }
    
    &:deep(.van-card__content) {
      min-height: 80px;
      display: flex;
      flex-direction: column;
      justify-content: space-between;
    }
    
    &:deep(.van-card__title) {
      font-size: 16px;
      font-weight: 500;
      color: #333;
      margin-bottom: 4px;
      line-height: 1.4;
    }
    
    &:deep(.van-card__desc) {
      font-size: 13px;
      color: #666;
      margin-bottom: 8px;
    }
    
    &:deep(.van-card__price) {
      font-size: 18px;
      font-weight: 600;
      color: #ee0a24;
    }
    
    &:deep(.van-card__footer) {
      padding: 8px 12px;
      background: #fafafa;
      border-top: 1px solid #f0f0f0;
    }
  }
}

.order-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  margin-bottom: 8px;
  
  .order-tag {
    font-size: 10px;
    padding: 2px 6px;
  }
}



.order-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  
  
  
  .confirm-section {
    flex-shrink: 0;
    
    .confirm-checkbox {
      &:deep(.van-checkbox__icon) {
        font-size: 18px;
      }
      
      &:deep(.van-checkbox__label) {
        margin-left: 6px;
      }
    }
    
    .confirm-text {
      font-size: 14px;
      color: #666;
      font-weight: 500;
    }
  }
}

.order-item-error {
  height: 120px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #f5f5f5;
  border-radius: 8px;
  margin-bottom: 12px;
}

// 响应式设计
@media (max-width: 768px) {
  .order-item-card {
    .order-card {
      &:deep(.van-card__title) {
        font-size: 15px;
      }
      
      &:deep(.van-card__price) {
        font-size: 16px;
      }
    }
    
    .order-footer {
      .quantity-control {
        &:deep(.van-stepper) {
          height: 26px;
          
          .van-stepper__minus,
          .van-stepper__plus {
            width: 26px;
            height: 26px;
          }
          
          .van-stepper__input {
            height: 26px;
            font-size: 13px;
          }
        }
      }
    }
  }
}
</style>